<template>
  <div class="mapType" :style="{ left, bottom }" @click="handleClickShowLegend">
    <img class="mapTypeIcon" :class="{greyIcon: !state.isShowMapTypeList}" src="./icon.png" />
    <div class="mapTypeListBox" v-show="state.isShowMapTypeList">
      <ul class="mapTypeList">
        <li class="mapTypeCard" :class="{isActive: state.typeIndex === 1}" @click.stop="handleClickMapType(1)">
          <span>矢量图</span>
        </li>
        <li class="mapTypeCard" :class="{isActive: state.typeIndex === 2}" @click.stop="handleClickMapType(2)">
          <span>影像图</span>
        </li>
        <li class="mapTypeCard" :class="{isActive: state.typeIndex === 3}" @click.stop="handleClickMapType(3)">
          <span>地形图</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { reactive } from 'vue';
  import { setLayerVisible } from 'gmap-ol';
  import { getGlobalMap } from '@/components/GlobalMap';

  defineOptions({ name: 'MapType' });

  defineProps({
    left: {
      type: String,
      default: '-50px',
    },
    bottom: {
      type: String,
      default: '50px',
    },
  });

  const state = reactive({
    isShowMapTypeList: false,
    typeIndex: 1,
  });

  const handleClickShowLegend = () => {
    state.isShowMapTypeList = !state.isShowMapTypeList
  }

  // 点击切换地图底图
  const handleClickMapType = (typeIndex) => {
    const olMap = getGlobalMap();
    state.typeIndex = typeIndex;
    setLayerVisible(olMap, 'tiandituTileLayer', false);
    // setLayerVisible(olMap, 'tiandituRoadNet', false);
    setLayerVisible(olMap, 'tiandituSatellite', false);
    // setLayerVisible(olMap, 'tiandituSatelliteRoadNet', false);
    setLayerVisible(olMap, 'tiandituTerrain', false);
    setLayerVisible(olMap, 'tiandituTerrainRoadNet', false);
    if (typeIndex === 1) {
      setLayerVisible(olMap, 'tiandituTileLayer', true);
      // setLayerVisible(olMap, 'tiandituRoadNet', true);
    }
    if (typeIndex === 2) {
      setLayerVisible(olMap, 'tiandituSatellite', true);
      // setLayerVisible(olMap, 'tiandituSatelliteRoadNet', true);
    }
    if (typeIndex === 3) {
      setLayerVisible(olMap, 'tiandituTerrain', true);
      setLayerVisible(olMap, 'tiandituTerrainRoadNet', true);
    }
  };
</script>

<style lang="less" rel="stylesheet/less" scoped>
  .mapType {
    width: 40px;
    height: 40px;
    background: rgba(251, 252, 255, 1);
    box-shadow: 0px 4px 6px 0px #03225917;
    border-radius: 6px;
    border: 1px solid #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    &:hover, &:active {
      // background: #e8fff4;
      border: 1px solid #b9e9d3;
    }
    img.mapTypeIcon {
      width: 24px;
      height: 24px;
      cursor: pointer;
    }
    .greyIcon {
      filter: grayscale(100%);
    }
    .mapTypeListBox {
      position: absolute;
      right: 50px;
      width: 286px;
      background: rgba(251, 252, 255, 1);
      box-shadow: 0px 4px 6px 0px #03225917;
      border-radius: 6px;
      border: 1px solid #fff;
      padding: 10px;
      ul.mapTypeList {
        display: flex;
        align-items: center;
        height: 60px;
        li.mapTypeCard {
          width: 88px;
          height: 60px;
          border-radius: 2px;
          position: relative;
          box-sizing: border-box;
          border: 1px solid transparent;
          background: url(./maptype.png) no-repeat 0 0;
          background-size: cover;
          -webkit-transition-property: right, background-image;
          transition-property: right, background-image;
          span {
            position: absolute;
            bottom: 0;
            right: 0;
            display: inline-block;
            padding: 4px 4px 14px 4px;
            font-size: 12px;
            height: 12px;
            line-height: 12px;
            color: #fff;
            border-top-left-radius: 4px;
            background-color: rgba(0, 0, 0, 0.4);
          }
        }
        .mapTypeCard:nth-child(1){
          background: url(./maptype.png) no-repeat 0 -1px;
          background-size: cover;
        }
        .mapTypeCard:nth-child(2){
          background: url(./maptype.png) no-repeat 0 -61px;
          background-size: cover;
        }
        .mapTypeCard:nth-child(3){
          background: url(./maptype.png) no-repeat 0 -121px;
          background-size: cover;
        }
        /* .mapTypeCard:nth-child(1){
          background: url(./maptype.png) no-repeat 0 -181px;
          background-size: cover;
        } */
        .isActive {
          border: 1px solid #409fff !important;
          span{
            background-color: #409fff !important;
            color: #fff !important;
          }
        }
        .isShow {
          filter: grayscale(100%) !important;
          opacity: 0.7 !important;
        }
      }
    }
  }
</style>
